package com.qibo.baselib.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 计算工具
 */
public class CalculateUtils {

    public static void calculate(Context mContext, String keyType, String textValue, String editText , List<String> numList , int numIndex, EditText editMemberAmount){
        if(keyType.equals("num")){
            //数字键
            try{
                String orgValue = numList.get(numIndex);
                if(editText.equals("0")){
                    editMemberAmount.setText(textValue);
                }else{
                    //小数点最多只能后两位
                    if(orgValue.equals("0.00")){
                        orgValue=textValue;
                        numList.set(numIndex, textValue);
                        editMemberAmount.setText(textValue);
                        return;
                    }
                    if(orgValue.lastIndexOf(".")!=-1 && orgValue.lastIndexOf(".")<orgValue.length()-2){
                        Toast.makeText(mContext,"只能输入小数点后两位",Toast.LENGTH_SHORT).show();
                    }else{
                        if(editText.length()>=18){
                            Toast.makeText(mContext,"一次输入算式无法超过18个字符",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        int numLen = orgValue.length();
                        if(orgValue.lastIndexOf(".")!=-1){
                            numLen = orgValue.lastIndexOf(".")+1;
                        }
                        if(numLen==7){
                            Toast.makeText(mContext,"收款金额不能超过7位数",Toast.LENGTH_SHORT).show();
                        }else{
                            numList.set(numIndex, orgValue+textValue);
                            editMemberAmount.setText(editText+textValue);
                        }
                    }
                }
            }catch(Exception e){
                numIndex=0;
                numList=new ArrayList<String>();
                numList.add(textValue);
                editMemberAmount.setText(textValue);
            }
        }
        if(keyType.equals("back")){
            //退格
            try{
                if(editText.lastIndexOf("+") == editText.length()-1){
                    //最后为加则减少一位
                    numList.set(numIndex,"");
                    numIndex--;
                    editMemberAmount.setText(editText.substring(0,editText.length()-1));
                }else{
                    String orgValue = numList.get(numIndex);
                    if(!orgValue.equals("")){
                        numList.set(numIndex, orgValue.substring(0,orgValue.length()-1));
                    }
                    if(editText.equals("0")||editText.length()==1){
                        editMemberAmount.setText("0.00");
                        numList.set(numIndex,"0.00");
                    }else{
                        editMemberAmount.setText(editText.substring(0,editText.length()-1));
                    }
                }
            }catch(Exception e){
                numIndex=0;
                numList=new ArrayList<String>();
                numList.add("0");
                editMemberAmount.setText("");
            }
        }
        if(keyType.equals("point")){
            //小数点
            try{
                String orgValue = numList.get(numIndex);
                if(orgValue.lastIndexOf(".")!=-1){
                    Toast.makeText(mContext,"已存在小数点",Toast.LENGTH_SHORT).show();
                }else{
                    if(editText.lastIndexOf("+") == editText.length()-1){
                        //最后为加则补0
                        numList.set(numIndex, orgValue+"0"+textValue);
                        editMemberAmount.setText(editText+"0"+textValue);
                    }else{
                        numList.set(numIndex, orgValue+textValue);
                        editMemberAmount.setText(editText+textValue);
                    }
                }
            }catch(Exception e){
                numIndex=0;
                numList=new ArrayList<String>();
                numList.add("0.");
                editMemberAmount.setText("0.");
            }
        }
        if(keyType.equals("plus")){
            //加
            try{
                if(editText.lastIndexOf("+") == editText.length()-1){
                    //最后为加则不处理

                }else{
                    numIndex++;
                    numList.add("");
                    editMemberAmount.setText(editText+textValue);
                }
            }catch(Exception e){
                numIndex=0;
                numList=new ArrayList<String>();
                numList.add("0");
                editMemberAmount.setText("");
            }
        }
        if(keyType.equals("cancel")){

        }
        if(keyType.equals("equal")||keyType.equals("pay")){
            //等于
            try{
                double sum = 0.0;
                for(String value :numList){
                    if(!value.equals("")){
                        sum+= Double.parseDouble(value)+0.00d;
                    }
                }

                String sumStr=new DecimalFormat("##.##").format(sum);
                if(!sumStr.contains(".")){
                    sumStr+=".00";
                }
                if(sumStr.substring(sumStr.lastIndexOf(".")+1).length()<2){
                    sumStr+="0";
                }

                numIndex=0;
                numList=new ArrayList<String>();
                numList.add(sumStr);
                editMemberAmount.setText(sumStr);
            }catch(Exception e){
                numIndex=0;
                numList=new ArrayList<String>();
                numList.add("0");
                editMemberAmount.setText("");
            }
        }
        if(keyType.equals("pay")){
            //收款
            if (editMemberAmount.getText().toString().length() < 1||editMemberAmount.getText().toString().equals("0.00") || editMemberAmount.getText().toString().equals("0")) {
                Toast.makeText(mContext,"请输入金额",Toast.LENGTH_SHORT).show();
                return;
            }else {
                Toast.makeText(mContext,"收款成功",Toast.LENGTH_SHORT).show();
            }


        }

    }

}
